<?php

namespace app\admin\controller;

use app\common\model\Changguan as ChangguanModel;
use app\common\model\ChangguanChangdi;
use app\common\model\ChangguanFenleiChangdi as ChangguanFenleiChangdiModel;

class Changguan extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 分页获取班级信息
     * @return void
     */
    public function list()
    {
        //查询条件
        $where = array();

        if (input('name')) {
            $where['name'] = array('like', "%" . input('name') . "%");
        }

        //分页信息
        $page = max(input('page'), 1);

        $size = input('limit',20);
        //获取分页列表
        $list = ChangguanModel::getPage($where, $page, $size, 'id desc');
        //总页码
        $total = ChangguanModel::getCount($where);
        //数据返回
        if (!$list) {
            return returnJson(0, '', array('list' => [], 'total' => 0));
        }

        $fl_cd = ChangguanFenleiChangdiModel::getColumn([],'id,label');
        foreach ($list as &$v) {
            $fenlei_changdi = ChangguanChangdi::getAll(array('cgid'=>$v['id']));
            $v['label'] = [];
            foreach ($fenlei_changdi as $vv) {
                $fl = isset($fl_cd[$vv['flid']]) ? $fl_cd[$vv['flid']] : '';
                $cd = isset($fl_cd[$vv['cdid']]) ? $fl_cd[$vv['cdid']] : '';
                $v['label'][] = $fl . '-' .$cd;
            }
        }
        return returnJson(0, '', [
            'list' => $list,
            'total' => $total
        ]);
    }

    /**
     * 所有场馆
     * @return \think\response\Json
     */
    public function alllist()
    {

        $list = ChangguanModel::getColumn([],'id,name');

        //返回信息
        return returnJson(0, '', $list);
    }


    /**
     * 信息
     * @return \think\response\Json
     */
    public function detail()
    {
        //查询条件
        $where = array('id' => array('eq', input('id')));

        //获取信息
        $detail = ChangguanModel::getOne($where);

        //区域信息
        $detail['area'] = [$detail['province'],$detail['city'],$detail['county']];

        //类型和产地信息
        $chandi = ChangguanChangdi::getAll(['cgid'=>$detail['id']]);
        foreach ($chandi as $v) {
            $detail['changdi'][] = array($v['flid'],$v['cdid']);
        }

        //返回信息
        return returnJson(0, '', $detail);
    }


    /**
     * 新增或修改班级
     * @return \think\response\Json|void
     */
    public function edit()
    {
        if (request()->isPost()) {
            $input = input();
            $time = time();
            $area = input('area/a');
            $changdi = input('changdi/a');
            $data = array(
                'name' => $input['name'],
                'phone' => $input['phone'],
                'province' => $area[0],
                'city' => $area[1],
                'county' => $area[2],
                'address' => $input['address'],
                'update_time' => $time,
                'thumb' => $input['thumb']
            );
            if ($input['id']) {
                $data['id'] = $input['id'];
            } else {
                $data['create_time'] = $time;
            }

            if ($data['id']) {
                $res = ChangguanModel::update($data, array('id' => $data['id']));
                $res = $data['id'];
            } else {
                $res = ChangguanModel::setInsertGetId($data);
            }
            $cd_save = [];
            foreach ($changdi as $v){
                $cd_save[] = array(
                    'cgid'=>$res,
                    'flid' => $v[0],
                    'cdid'=>$v[1],
                    'update_time'=>time(),
                    'create_time'=>time()
                );
            }
            ChangguanChangdi::setDel(array('cgid'=>$res));
            ChangguanChangdi::setSaveAll($cd_save);

            if ($res) {
                return returnJson(0, '操作成功');
            }
            return returnJson(1, '网络异常，请重试');
        }
    }

    /**
     * 删除场馆
     * @return \think\response\Json
     */
    public function delete()
    {
        if (input('id', 0)) {
            //查询条件
            $where['id'] = input('id');

            $detail = ChangguanModel::getOne($where);

            //删除记录
            ChangguanModel::setDel($where);

            //删除图片
            model('qiniu')->remove($detail['thumb']);//删除图集

            //删除分类和场地
            ChangguanChangdi::setDel(array('cgid'=>input('id')));
        }
        return returnJson(0, '');

    }



}
